package com.tuanzi.loan.web.bean.collaborate;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.statistics.AdverChannelTypeUser;
import com.tuanzi.loan.business.service.collaborate.CollaBorateAdvertiseTransFormationService;
import com.tuanzi.loan.business.service.statistics.AdverChannelTypeUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.advertise.AdvertiseTransStatRequest;
import com.tuanzi.loan.vo.response.advertise.AdvertiseTransStatResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.collaborate.CollaBorateAdvertiseTransStatDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.ADS_PLATFORM_TYPE;

/**
 * 广告回调统计
 *
 * created by chuIllusions_tan 20180727
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CollaBorateAdvertiseTransStatBean extends JSFListingBean<AdvertiseTransStatRequest, AdvertiseTransStatResponse>{


    @Autowired
    private CollaBorateAdvertiseTransFormationService advertiseTransStatService;

    @Autowired
    private AdverChannelTypeUserService adverChannelTypeUserService;

    private List<AdverChannelTypeUser> currentUserChannelList;

    private Set<Integer> adsPlatformTypeLists;
    private StreamedContent xlsFile;

    private Long sum = 0L;

    public StreamedContent downloadExcel() throws UnsupportedEncodingException {
        InputStream input = new ByteArrayInputStream(advertiseTransStatService.getExcel($).getBytes("GBK"));
        String fileName = System.currentTimeMillis() + ".csv";
        xlsFile = new DefaultStreamedContent(input, "application/vnd.ms-excel;charset=gb2312", fileName);
        return xlsFile;
    }

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new AdvertiseTransStatRequest();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 初始化默认当前日期
        Date date = null;
        try {
            date = sdf.parse(sdf.format(new Date()));
            $.setStartDate(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        lazyDataModel = new CollaBorateAdvertiseTransStatDataModel($);
        initChannels();
        initAdsPlatformType();
    }
    private void initChannels(){
        if(null == currentUserChannelList || currentUserChannelList.size() == 0){
            currentUserChannelList = adverChannelTypeUserService.findCurrentUserChannels();

        }
    }
    private void initAdsPlatformType(){
        if(null == currentUserChannelList || currentUserChannelList.size() == 0){
            currentUserChannelList = adverChannelTypeUserService.findCurrentUserChannels();
            adsPlatformTypeLists  = currentUserChannelList.stream().map(AdverChannelTypeUser::getAdsPlatformType).collect(Collectors.toSet());
        }
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(ADS_PLATFORM_TYPE);
    }
    public void getCountSum(){
        sum = 0L;
        sum = advertiseTransStatService.getCountSum();
    }
}
